package com.example.review.Fragment;


import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.example.review.Entity.Lights;
import com.example.review.R;
import com.example.review.Util.RetrofitUtil;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.GET;

/**
 * A simple {@link Fragment} subclass.
 */
public class SortFragment extends Fragment {

    private List<Lights> list;
    @BindView(R.id.spinner)
    Spinner spinner;
    @BindView(R.id.list_item)
    ListView listItem;

    public SortFragment() {
        // Required empty public constructor
    }

    private interface BlogServer {
        @GET("getTrafficLightRGY")
        Call<ResponseBody> getBlog();

    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_sort, container, false);
        ButterKnife.bind(this,view);
        initData();
        return view;


    }

    private void initReData() {
        String[] strings = getResources().getStringArray(R.array.sort);
        ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<String>(getActivity(),R.layout.support_simple_spinner_dropdown_item,strings);
        spinner.setAdapter(stringArrayAdapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                List<Lights.DataBean> data = list.get(0).getData();
                sorts(data,i);
                listItem.setAdapter(new myAdapter(data));
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

    public void initData() {
        BlogServer blogServer = RetrofitUtil.initialize("http://hh1.me:5001/").create(BlogServer.class);
        new RetrofitUtil(blogServer.getBlog()) {
            @Override
            public void onFinish(Call<ResponseBody> call, Response<ResponseBody> response) {
                String string = null;
                try {
                    string = response.body().string();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Log.v("TAG","asf:"+string);
                Lights lights = new Gson().fromJson(string, Lights.class);
                list = new ArrayList<>();
                list.add(lights);
                myAdapter myAdapter = new myAdapter(list.get(0).getData());
                listItem.setAdapter(myAdapter);
                initReData();
            }

            @Override
            public void onError(Call<ResponseBody> call, Throwable t) {

            }
        }.sendRequest();
    }
    private void sorts(List<Lights.DataBean> list,int s)  {
        Collections.sort(list, new Comparator<Lights.DataBean>() {
            @Override
            public int compare(Lights.DataBean t0, Lights.DataBean t1) {
                int i=0;
                if(s==0||s==1){i = t0.getRoad()-t1.getRoad();}else
                if(s==2||s==3){i = t0.getRed()-t1.getRed();}else
                if(s==4||s==5){i = t0.getGreen()-t1.getGreen();}else
                if(s==6||s==7){i = t0.getYellow()-t1.getYellow();}
                return i;
            }
        });
        if(s==1||s==3||s==5||s==7){
        Collections.reverse(list);
        }

    }
    public class myAdapter extends BaseAdapter {
        List<Lights.DataBean> list;

        public myAdapter(List<Lights.DataBean> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return i;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }
        public class ViewHolder {
            @BindView(R.id.luko)
            TextView luko;
            @BindView(R.id.hongd)
            TextView hongd;
            @BindView(R.id.lvd)
            TextView lvd;
            @BindView(R.id.huangd)
            TextView huangd;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder holder;
            if(view==null){
                view = LayoutInflater.from(getContext()).inflate(R.layout.layout_list, viewGroup, false);
                holder = new ViewHolder(view);
                view.setTag(holder);
            }else {
                holder= (ViewHolder) view.getTag();
            }
            holder.luko.setText(list.get(i).getRoad()+"");
            holder.hongd.setText(list.get(i).getRed()+"");
            holder.huangd.setText(list.get(i).getYellow()+"");
            holder.lvd.setText(list.get(i).getGreen()+"");
            return view;
        }
    }
}
